package hoowin.common.action;

import hoowin.common.model.FileItem;
import hoowin.common.service.FileService;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class Download extends ActionSupport {
	private FileItem fileItem = new FileItem();
	private InputStream inputStream;
	private FileService fileService;
	private String message;
	
	@Override
	public String execute(){
		String info = fileService.prepareDownload(fileItem);
		if(info.equals(SUCCESS)){
			try {
				inputStream = new FileInputStream(fileItem.getItem());
				return SUCCESS;
			} catch (FileNotFoundException e) {
				setMessage(getText("exception.1100",fileItem.getItemFileName(),fileItem.getId()+""));
				return ERROR;
			}
		}else{
			setMessage(getText(info));
			return ERROR;
		}
		
	}

	public void setFileId(int fileId) {
		this.fileItem.setId(fileId);
	}
	public void setFileKey(String fileKey){
		this.fileItem.setKey(fileKey);
	}
	public FileItem getFileItem() {
		return fileItem;
	}
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	public InputStream getInputStream() {
		return inputStream;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getMessage() {
		return message;
	}

	public void setFileService(FileService fileService) {
		this.fileService = fileService;
	}
}
